//index.js
//获取应用实例
const app = getApp()
var dateTimePicker = require('../../utils/dateTimePicker.js');

Page({
  data: {
    // 套餐id
    appointment_id: "",
    // 套餐名称
    package: "",
    name: "",
    phone: "",
    // 车辆型号
    type: "",
    // 车牌号
    carid: "",
    // 小区信息
    areaArray: [],
    areaIndex: 0,

    // 日期选择器
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2021,
    endYear: 2050
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: "预约信息填写"
    });
    var that = this;
    var token = wx.getStorageSync('token')
    var appointment_id = wx.getStorageSync('appointment_id');
    var mealName = wx.getStorageSync('mealName')
    console.log(mealName)
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(that.data.startYear, that.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(that.data.startYear, that.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();
    that.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      package: mealName,
      appointment_id: appointment_id,
      name: wx.getStorageSync('username')
    });
    wx.request({
      url: 'http://localhost:8080/system/community/list',  // 获取社区
      method: 'get',
      header: {
        'content-type': 'application/json',
        'Authorization': token
      },
      success(res) {
        that.setData({
          areaArray: res.data.rows,
        })
      }
    })
  },
  bindDateChange: function (e) {
    var that = this;
    that.setData({
      date: e.detail.value
    })
    console.log(that.data.date);
  },
  // 监听输入手机号
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 监听输入车辆型号
  typeInput: function (e) {
    this.setData({
      type: e.detail.value
    })
  },
  // 监听输入车牌号
  caridInput: function (e) {
    this.setData({
      carid: e.detail.value
    })
  },
  // 监听选择小区
  areaInput: function(e) {
    this.setData({
      areaIndex: e.detail.value
    })
  },

  //提交信息
  formSubmit: function (e) {
    //表单校验
    if(!this.checkForm(e)){
        return false;
    }
    var that = this;
    var token = wx.getStorageSync('token')
    var datayear = that.data.dateTimeArray1[0][that.data.dateTime1[0]]
    var datamonth = that.data.dateTimeArray1[1][that.data.dateTime1[1]]
    var dataday = that.data.dateTimeArray1[2][that.data.dateTime1[2]]
    var datah = that.data.dateTimeArray1[3][that.data.dateTime1[3]]
    var datas = that.data.dateTimeArray1[4][that.data.dateTime1[4]]
    var realdata = datayear+"-"+datamonth+"-"+dataday
    console.log(realdata)
    //提交表单
    wx.request({
      url: 'http://127.0.0.1:8080/system/appointment',//提交路径
      method: 'post',
      header: {
        'Content-Type': 'application/json',
        'Authorization': token
      },
      data: {
        username: that.data.name,
        mealName: that.data.package,
        mealId: that.data.appointment_id.toString(),
        communityName: that.data.areaArray[that.data.areaIndex].communityName,
        model: that.data.type,
        carNum: that.data.carid,
        arrivalTime: realdata,
        ststus: 0
      },
      success: function (res) { 
        console.log(res.data)
        if (res.data.code==200){
          wx.setStorageSync('phone', that.data.phone)
          wx.setStorageSync('model', that.data.type)
          wx.setStorageSync('carNum', that.data.carid)
          wx.showToast({
            title: '预约成功',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
          setTimeout(() => {
            wx.reLaunch({
              url: "/pages/index/index"
            })
          }, 1000)
        }else{
          wx.showToast({
            title: '服务器忙，请稍后再试',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        }
      }
    })
  },
  checkForm:function(e){
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(e.detail.value.phone)) {
      wx.showToast({
        title: '手机号格式有误',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return false;
    }
    return true;
  },
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
    console.log(e.detail.value)
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
})
